

import 'package:erp/Entity/Product.dart';
import 'package:erp/Entity/ServerResponse.dart';
import 'package:erp/Entity/SignInResult.dart';
import 'package:erp/Events/EventAddrs.dart';
import 'package:erp/Store/AccountStore.dart';
import 'package:erp/Store/ProductStore.dart';
import 'package:erp/Store/ProductsInWareHouseStore.dart';
import 'package:erp/global.dart';

void registerFirstDataPrepareEvent(){

  /**
   * Prepare First product lists
   */
  globalBus.subscribe<ServerResponse<SignInResult>>(Events.ACCOUNT_SIGNIN_SUCCESS, (bus, data) async {
    globalBus.publish(Events.PRODUCT_DATA_SELECT_REQUEST, data);
  });

  /**
   * prepare Product types
   */
  globalBus.subscribe<ServerResponse<SignInResult>>(Events.ACCOUNT_SIGNIN_SUCCESS, (bus, data) async {
    globalBus.publish(Events.PRODUCTTYPE_SELECT_REQUEST, data);
  });
  globalBus.subscribe(Events.PRODUCT_IN_WAREHOUSE_SELECT_REQUEST, (bus, _)async {
    try {
      var result = await externalInterface.getAllProductsInWareHouse(AccountStore.signInResult.token);
      ProductsInWareHouseStore.products = result;
      bus.publish(Events.PRODUCT_IN_WAREHOUSE_SELECT_SUCCESS, result);
    }
    catch (exception){
      bus.publish(Events.PRODUCT_IN_WAREHOUSE_SELECT_ERROR, exception);
    }
  });
  globalBus.subscribe<ServerResponse<SignInResult>>(Events.ACCOUNT_SIGNIN_SUCCESS, (bus, data) async {
    globalBus.publish(Events.SUPPLIER_SELECT_REQUEST, null);
  });

  globalBus.subscribe<ServerResponse<SignInResult>>(Events.ACCOUNT_SIGNIN_SUCCESS, (bus, data) async {
    globalBus.publish(Events.SUPPLIER_TYPE_SELECT_REQUEST, null);
  });

  globalBus.subscribe(Events.ACCOUNT_SIGNIN_SUCCESS, (bus, _){
    globalBus.publish(Events.WAREHOUSE_SELECT_REQUEST, null);
  });

  globalBus.subscribe(Events.ACCOUNT_SIGNIN_SUCCESS, (bus, _){
    globalBus.publish(Events.PURCHASE_SELECT_REQUEST, null);
  });

  globalBus.subscribe(Events.ACCOUNT_SIGNIN_SUCCESS, (bus, _){
    globalBus.publish(Events.CUSTOMER_TYPE_SELECT_REQUEST, null);
  });

  globalBus.subscribe(Events.ACCOUNT_SIGNIN_SUCCESS, (bus, _){
    globalBus.publish(Events.CUSTOMER_SELECT_REQUEST, null);
  });

  globalBus.subscribe(Events.ACCOUNT_SIGNIN_SUCCESS, (bus, _){
    bus.publish(Events.SALES_ORDER_SELECT_REQUEST, null);
  });

  globalBus.subscribe(Events.ACCOUNT_SIGNIN_SUCCESS, (bus, _){
    bus.publish(Events.PRODUCT_IN_WAREHOUSE_SELECT_REQUEST, null);
  });
}